home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 904 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.7 KB

  1. Path: engnews1.Eng.Sun.COM!taumet!clamage
  2. From: clamage@Eng.sun.com (Steve Clamage)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: Rationale behind disallowal of non-const r
  5. Date: 28 Mar 1996 21:16:23 GMT
  6. Organization: Sun Microsystems Inc.
  7. Approved: clamage@eng.sun.com (comp.std.c++)
  8. Message-ID: <4jevb1$kkp@engnews1.Eng.Sun.COM>
  9. References: <DAVEW.96Mar27195129@trigati.cs.haverford.edu>
  10. Reply-To: clamage@Eng.sun.com
  11. NNTP-Posting-Host: taumet.eng.sun.com
  12. X-Nntp-Posting-Host: taumet.eng.sun.com
  13. Content-Length: 2840
  14. X-Lines: 60
  15. Originator: clamage@taumet
  16.  
  17. In article 96Mar27195129@trigati.cs.haverford.edu, davew@trigati.cs.haverford.edu (David G. Wonnacott) writes:
  18. >
  19. >I am looking for an explanation for the rationale behind the decision
  20. >of the standards committee to disallow the binding of a non-const
  21. >reference to an rvalue.
  22.  
  23. The basic reason is that it usually is an error. If you have a non-const,
  24. (rather than a const) reference, the presumption is that you are likely to
  25. modify the referenced object. Modifying an rvalue is not likely to be
  26. what was intended. Two examples:
  27.  
  28.     double& rd = 2.0; // #1
  29.  
  30. In #1, we have a reference to ... what? "2.0" isn't an object, it is a
  31. value. What should it mean to assign to rd? Why not write
  32.     double rd = 2.0; // rd is now an object
  33. instead? If you don't intend to assign to rd, make it a reference to const.
  34.     const double& rd = 2.0; // OK
  35. This case is rather minor. The main problem shows up in the next example.
  36.  
  37.     void f(double& rd);
  38.     int k = ...;
  39.     f(k); // #2
  40.  
  41. In #2, we should assume that function f is going to modify its actual
  42. argument. But the actual argument has type int. An int can be converted
  43. to a double, so the compiler could create a temp double, and pass a
  44. reference to the temp. If f is declared to take a const reference, that is
  45. what happens. That can't cause a problem, since the value can't be changed by
  46. function f. But if f assigns to its non-const actual argument, the assumption
  47. is that the actual argument is changed; that won't happen in this case. The
  48. value of k is unaffected by anything that happens in f.
  49.  
  50. If you mean to allow passing an int to f, you have three choices: pass by
  51. value instead of reference, or declare the formal parameter to be a reference
  52. to a const double, or create an explicit variable of the right type and
  53. initialize it with the value of k first:
  54.     double t = k;
  55.     f(t); // OK
  56. Any of these approaches makes it clear that k is not going to be changed.
  57.  
  58. For simplicity, I have shown examples with int and double, which will
  59. almost always have different sizes and representations. The arguments are
  60. even stronger with class types. Please also note that when I say things like
  61. "can't happen", I mean in a well-formed program.
  62.  
  63. Yes, there are times when you would like to be able to bind an rvalue
  64. to a non-const reference, since the only things you care about are
  65. sure to happen anyway. For example, given a class T:
  66.     T f1();     // f1 returns a T rvalue
  67.     f1().foo(); // ok only if foo is a const member function of T
  68. Sometimes you don't care about the effects of foo on the object here,
  69. and you only want the external side-effects of foo. I believe the C++
  70. committee looked for ways to allow this binding of an rvalue to a non-const
  71. reference in cases where the results were what was intended, but failed to
  72. find a good way to define those cases.
  73. ---
  74. Steve Clamage, stephen.clamage@eng.sun.com
  75.  
  76.  
  77.  
  78.  
  79. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  80. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  81. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  82. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  83. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  84.